package com.jh.paymentchannel.util.xjy;

import org.jetbrains.annotations.Nullable;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;


public class PlatService {

    public static Map<String, String> post(
            Map<String, String> reqData, String reqUrl, Charset charset) throws Exception {
        Map<String, String> rspData = new HashMap<String, String>();
        //发送后台请求数据
        HttpClient hc = new HttpClient(reqUrl, 30000, 30000);
        try {
            int status = hc.send(reqData, charset);
            if (200 == status) {
                String resultString = hc.getResult();
                if (null != resultString && !"".equals(resultString)) {
                    // 将返回结果转换为map
                    Map<String, String> tmpRspData = SDKUtil.convertResultStringToMap(resultString);
                    rspData.putAll(tmpRspData);
                }
            } else {
                throw new Exception("返回http状态码[" + status + "]，请检查请求报文或者请求地址是否正确");
            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return rspData;
    }

    public static String postAsString(
            Map<String, String> reqData, String reqUrl, Charset charset) throws Exception {
        //发送后台请求数据
        HttpClient hc = new HttpClient(reqUrl, 30000, 30000);
        int status = hc.send(reqData, charset);
        String resultString = getResultString(hc, status);
        if (resultString != null) return resultString;
        return "查询失败";
    }

    @Nullable
    private static String getResultString(HttpClient hc, int status) throws Exception {
        if (200 == status) {
            String resultString = hc.getResult();
            if (null != resultString && !"".equals(resultString)) {
                return resultString;
            }
        } else {
            throw new Exception("返回http状态码[" + status + "]，请检查请求报文或者请求地址是否正确");
        }
        return null;
    }

    public static String get(String reqUrl, Charset charset) throws Exception {

        //发送后台请求数据
        HttpClient hc = new HttpClient(reqUrl, 30000, 30000);
        try {
            int status = hc.sendGet(charset);
            String resultString = getResultString(hc, status);
            if (resultString != null) return resultString;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
        return null;
    }

    public static String createAutoFormHtml(Map<String, String> hiddens, String reqUrl, Charset charset) {
        StringBuilder sf = new StringBuilder();
        sf.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=")
                .append(charset.displayName()).append("\"/></head><body>");
        sf.append("<form id = \"pay_form\" action=\"").append(reqUrl).append("\" method=\"post\">");
        if (null != hiddens && 0 != hiddens.size()) {
            Set<Entry<String, String>> set = hiddens.entrySet();
            for (Entry<String, String> ey : set) {
                String key = ey.getKey();
                String value = ey.getValue();
                sf.append("<input type=\"hidden\" name=\"").append(key).append("\" id=\"")
                        .append(key).append("\" value=\"").append(value).append("\"/>");
            }
        }
        sf.append("</form>");
        sf.append("</body>");
        sf.append("<script type=\"text/javascript\">");
        sf.append("document.all.pay_form.submit();");
        sf.append("</script>");
        sf.append("</html>");
        return sf.toString();
    }
}
